﻿@inject IPopupService PopupService

<MRow>
    <MCol Cols="12" Md="6">
        <MCheckbox @bind-Value="Model.Open" Label="开启预约" TValue="bool" />
    </MCol>
    @if (Model.Open)
    {
        <MCol Cols="12" Md="6">
            <MCheckbox @bind-Value="Model.IsNeedNotification" Label="上线后通知预约用户" TValue="bool" />
        </MCol>
        <MCol Cols="12" Md="6">
            <CnGalWebSite.Components.Inputs.MasaAutoComplete @bind-Value="@Model.LotteryName" Label="关联抽奖" AutoCompleteItems="LotteryItems" />
        </MCol>

    }
</MRow>
@if (Model.Open)
{
    <CnGalWebSite.Components.Buttons.MasaButton Text="添加目标" OnClick="OnClickInput" Icon="@IconType.Goal.ToIconString()" Class="mb-4" />
    @if(Model.Goals.Any())
    {
            <MSimpleTable Class="mb-4">
        <thead>
            <tr>
                <th class="text-left">
                    目标人数
                </th>
                <th class="text-left">
                    名称
                </th>
                <th class="text-left">
                    操作
                </th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.Goals.OrderBy(s => s.Target))
            {
                <tr @key="item.Name">
                    <td style="min-width: 50px; ">@item.Target</td>
                    <td style="overflow-wrap: anywhere; min-width: 200px; ">@item.Name</td>
                    <td>
                        <CnGalWebSite.Components.Buttons.MasaButton Tooltip="编辑" OnClick="@(()=>OnEdit(item))" Icon="@IconType.Edit.ToIconString()" Color="success" />
                        <CnGalWebSite.Components.Buttons.MasaButton Tooltip="删除" OnClick="@(()=>OnDelete(item))" Icon="@IconType.Delete.ToIconString()" Color="error" />
                    </td>
                </tr>
            }
        </tbody>
    </MSimpleTable>

    }

    <BookingGoalInputDialog @ref="bookingGoalInputDialog" OnChanged="OnChanged" Items="Model.Goals" />
}

@code {
    [Parameter]
    public EditBookingModel Model { get; set; }
    [Parameter]
    public List<string> LotteryItems { get; set; }


    BookingGoalInputDialog bookingGoalInputDialog;

    public void OnClickInput()
    {
        if (bookingGoalInputDialog != null)
        {
            var goal = new EditBookingGoalModel();
            Model.Goals.Add(goal);
            bookingGoalInputDialog.Refresh(goal);
        }
    }

    public void OnDelete(EditBookingGoalModel model)
    {
        Model.Goals.Remove(model);
    }

    public void OnEdit(EditBookingGoalModel model)
    {
        if (bookingGoalInputDialog != null)
        {
            bookingGoalInputDialog.Refresh(model);
        }
    }

    public void OnChanged()
    {
        Model.Goals.RemoveAll(s => string.IsNullOrWhiteSpace(s.Name));
        StateHasChanged();
    }
}
